//
//  main.m
//  电话博文件解析
//
//  Created by mac on 16-5-10.
//  Copyright (c) 2016年 5-10. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
/*    
 @autoreleasepool {
        NSString *path=@"/Users/mac/Desktop/00001.vcf";
        NSError *err;
        NSString *vcf = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
        NSLog(@"%@",vcf);
        
        NSArray *arr=[vcf componentsSeparatedByString:@"\n"];
        
        for (NSString *line in arr)
        {
            if ([line hasPrefix:@"FN"])//找到以FN开头的行
            {
                NSArray *arr = [line componentsSeparatedByString:@":"];
                NSString *str = arr[1];
                str = [str stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
               // str = [str stringByReplacingOccurrencesOfString:@";"withString:@""];
                //NSLog(@"%@",str);
                str = [str stringByRemovingPercentEncoding];
                NSLog(@"%@",str);
            }
            
            if([line hasPrefix:@"TEL"])
            {
                NSArray *arr = [line componentsSeparatedByString:@":"];
                NSLog(@"%@",arr[1]);
            }
        }
    }*/
    @autoreleasepool {
        NSString *path = @"/Users/mac/Desktop/00001.vcf";
        NSError *err;
        
        NSString *vcf = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
        
        NSArray *arr = [vcf componentsSeparatedByString:@"\n"];
        
        NSMutableArray *linkman = [NSMutableArray new];
        NSMutableDictionary *dcit;
        
        for (NSString *line in arr)
        {
            if([line hasPrefix:@"BEGIN"])
            {
                dcit = [NSMutableDictionary new];
            }
            
            if([line hasPrefix:@"END"])
            {
                [linkman addObject:dcit];
            }
            
            if([line hasPrefix:@"FN"])
            {
                NSArray *arr1 = [line componentsSeparatedByString:@":"];
                [dcit setObject:arr1[1] forKey:@"NAME"];
            }
            
            if([line hasPrefix:@"TEL"])
            {
                NSArray *arr2 = [line componentsSeparatedByString:@":"];
                   NSString *str = [arr2[1] stringByReplacingOccurrencesOfString:@"-" withString:@""];
                [dcit setObject:str forKey:@"TEL"];
            }
        }
        
        [linkman writeToFile:@"/Users/mac/Desktop/联系人.txt" atomically:NO];
        
    }
    return 0;
}

